package game;
import game.io.TagIO;
import items.Item;

import java.io.File;
import java.util.ArrayList;
import java.util.StringTokenizer;

import room.Room;


public class Game
{
  private static TagIO tagIO = new TagIO();
  private ArrayList<Room> rooms;
  private ArrayList<Item> inventory;
  private Room currentRoom;

  public Game()
  {
    rooms = new ArrayList<Room>();
    inventory = new ArrayList<Item>();
  }
  
  public static Game load(String fileName)
  {
    return tagIO.XMLin(new File(fileName));
  }
  
  public void save(String fileName)
  {
    tagIO.XMLout(this, new File(fileName));
  }
  
  public String executeVerb(String verb)
  {
    StringTokenizer st = new StringTokenizer(verb);
    if(st.countTokens() >= 2)
    {
      String action = st.nextToken();
      if(action.equals("take"))
      {
        String itemName = st.nextToken();
        ArrayList<Item> items = currentRoom.getItems();
        for(Item i : items)
          if(i.getName().equals(itemName))
          {
            return i.executeVerb("onTake");
          }
        return "There is no " + itemName + " to take.\n";
      }
      else if(action.equals("use"))
      {
        String itemName = st.nextToken();
        for(Item i : inventory)
          if(i.getName().equals(itemName))
          {
            return currentRoom.useItem(i);
          }
        return "There is no " + itemName + " to use.\n";
      }
      else if(action.equals("list"))
      {
        String container = st.nextToken();
        String list = "";
        int num = 1;
        if(container.equals("inventory"))
        {
          if(inventory.size() == 0)
          {
            list = "Your inventory is empty.\n";
          }
          else
          {
            for(Item i : inventory)
            {
              list += num + " : " + i.getName() + "\n";
              num++;
            }
          }
        }
        else if(container.equals("items"))
        {
          ArrayList<Item> items = currentRoom.getItems();
          if(items.size() == 0)
          {
            list = "There are no items.\n";
          }
          else
          {
            for(Item i : items)
            {
              list += num + " : " + i.getName() + "\n";
              num++;
            }
          }
        }
        return list;
      }
      else
      {
        String itemName = st.nextToken();
        for(Item i : inventory)
          if(i.getName().equals(itemName))
          {
            return i.executeVerb(action);
          }
        
        return currentRoom.executeVerb(verb);
      }
    }
    else
      return currentRoom.executeVerb(verb);
  }
  
  public Room createRoom()
  {
    Room newRoom = new Room();
    rooms.add(newRoom);
    return newRoom;
  }
  
  public boolean deleteRoom(Room room)
  {
    return rooms.remove(room);
  }
  
  public boolean deleteRoom(int id)
  {
    for(Room room : rooms)
    {
      if(room.getId() == id)
        return rooms.remove(room);
    }
    return false;
  }
  
  public Room getRoom(int id)
  {
    for(Room room : rooms)
    {
      if(room.getId() == id)
        return room;
    }
    return null;
  }
  
  /**
   * @return the rooms
   */
  public ArrayList<Room> getRooms()
  {
    return rooms;
  }
  
  /**
   * @return the currentRoom
   */
  public Room getCurrentRoom()
  {
    return currentRoom;
  }

  /**
   * @param currentRoom the currentRoom to set
   */
  public void setCurrentRoom(Room currentRoom)
  {
    this.currentRoom = currentRoom;
  }

  /**
   * @return the inventory
   */
  public ArrayList<Item> getInventory()
  {
    return inventory;
  }
}
